package m.jclecai.com.dialog;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.bean.RecordsBean;
import m.jclecai.com.listener.Callback;

/**
 * Created by zed on 2018/3/10.
 */

public class WaitPayOrderDialog extends BaseDialog {

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    Callback callback;
    List<RecordsBean> mData;
    WaitPayOrderAdapter adapter;

    public WaitPayOrderDialog(Context context) {
        super(context);
        initView();
    }

    public WaitPayOrderDialog(Context context, boolean fromButton) {
        super(context, fromButton);
        initView();
    }

    void initView() {
        setContentView(R.layout.dialog_wait_pay_order);
        mData = new ArrayList<>();
        adapter = new WaitPayOrderAdapter();
        rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        rvList.setAdapter(adapter);
    }

    @OnClick(R.id.btn_cancel)
    void close() {
        dismiss();
    }

    @OnClick(R.id.btn_submit)
    void submit() {
        dismiss();
        if (callback != null) callback.callback(mData.get(adapter.mCheckedPosition));
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void setData(List<RecordsBean> mData) {
        this.mData.clear();
        this.mData.addAll(mData);
        if (mData.size() > 0)
            this.mData.get(0).setChecked(true);
        adapter.notifyDataSetChanged();
    }

    class WaitPayOrderAdapter extends RecyclerView.Adapter<WaitPayOrderHolder> {

        public int mCheckedPosition;

        @Override
        public WaitPayOrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new WaitPayOrderHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_wait_pay_order, parent, false));
        }

        @Override
        public void onBindViewHolder(WaitPayOrderHolder holder, final int position) {

            holder.rb_check.setChecked(mData.get(position).isChecked());
            holder.tv_order_no.setText(mData.get(position).getProjectno());
            StringBuffer detail = new StringBuffer();
            detail.append("金额:" + mData.get(position).getAmount());
            detail.append("  ");
            detail.append("下单时间:" + mData.get(position).getAddtime());
            holder.tv_order_detail.setText(detail.toString());

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mData.get(mCheckedPosition).setChecked(false);
                    mCheckedPosition = position;
                    mData.get(mCheckedPosition).setChecked(true);
                    notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    class WaitPayOrderHolder extends RecyclerView.ViewHolder {
        RadioButton rb_check;
        TextView tv_order_no;
        TextView tv_order_detail;

        public WaitPayOrderHolder(View itemView) {
            super(itemView);
            rb_check = (RadioButton) itemView.findViewById(R.id.rb_check);
            tv_order_no = (TextView) itemView.findViewById(R.id.tv_order_no);
            tv_order_detail = (TextView) itemView.findViewById(R.id.tv_order_detail);
        }
    }
}
